package com.geezdata.cps.sdk3rd.wechat.method;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.geezdata.cps.base.utils.HttpClientUtil;
import com.geezdata.cps.sdk3rd.wechat.request.WechatLoginRequest;
import com.geezdata.cps.sdk3rd.wechat.response.WechatLoginResult;
public class WechatLogin extends WechatPay implements Wechat<WechatLoginRequest, WechatLoginResult> {
	private static final Logger logger = LoggerFactory.getLogger(WechatLogin.class);
	
	public static String Login_URL = "https://api.weixin.qq.com/sns/jscode2session";
	public static String GRANT_TYPE = "authorization_code";
	
	@Override
	public WechatLoginResult execute(WechatLoginRequest request) {
		String respJson =null;
		try {
			respJson = HttpClientUtil.get(Login_URL+"?appid="+request.getAppId()+"&secret="+
					request.getSecret()+"&js_code="+request.getJsCode()+"&grant_type="+GRANT_TYPE);
			logger.info(respJson);
			JSONObject jsonObject = JSONObject.parseObject(respJson);
			WechatLoginResult  result = JSONObject.toJavaObject(jsonObject,WechatLoginResult.class);
			return result;
		} catch (IOException e) {
			logger.error("WeixinLogin::execute failed url {}, params {}");
		}
		
		return null;
	}

}
